package cn.app.web.controller;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.app.dao.AppCategoryDao.AppCategoryDao;
import cn.app.dao.dictionaryDao.DatadictionaryDao;
import cn.app.entity.AppCategory;
import cn.app.entity.AppInfo;
import cn.app.entity.DataDictionary;
import cn.app.service.AppCategoryService;
import cn.app.service.AppInfoService;
import cn.app.service.DatadictionaryService;
import cn.app.utils.AppDictionaryFinal;
import cn.app.utils.PageBean;

@Controller
@RequestMapping("/applist")
public class AppInfoListController {
	// 字典表的service
	@Autowired
	private DatadictionaryService data_dictionaryService;
	// 三级分类
	@Autowired
	public AppCategoryService appCategoryService;
	// appinfoservice商品显示
	@Autowired
	public AppInfoService appInfoService;

	// 跳转到app主页main
	@RequestMapping("/appmain")
	public String appMain() {
		return "dev/mian";
	}

	// 跳转到app维护页面
	@RequestMapping("/appinfolist")
	public String appInfoList(Model model, Integer currentPage, String status,
			String softwarename, String flatformid, String categorylevel1,
			String categorylevel2, String categorylevel3) {

		// 1.1显示app状态下拉框
		List<DataDictionary> appStatuslist = data_dictionaryService
				.finall(AppDictionaryFinal.APP_STATUS);
		List<DataDictionary> appFlatformlist = data_dictionaryService
				.finall(AppDictionaryFinal.APP_FLATFORM);
		// 1.2把信息放入mode中返回给页面
		model.addAttribute("appStatuslist", appStatuslist);
		model.addAttribute("appFlatformlist", appFlatformlist);
		// 1.3三级分类
		List<AppCategory> appcategorylevel1 = appCategoryService
				.findall(AppDictionaryFinal.APP_CATEGORY_LEVE_ONE);
		model.addAttribute("appcategorylevel1", appcategorylevel1);
		// ===========================================================================================
		// 2.1显示app信息表格
		// 2.2更具条件查询信息
		Map<String, Object> applistmap = new HashMap();
		applistmap.put("currentpage", currentPage);
		applistmap.put("softwarename", softwarename);
		applistmap.put("status", status);
		applistmap.put("flatformid", flatformid);
		applistmap.put("categorylevel1", categorylevel1);
		applistmap.put("categorylevel2", categorylevel2);
		applistmap.put("categorylevel3", categorylevel3);
		model.addAttribute("applistmap", applistmap);
	//	判断数据是否为空 为空则为赋值 达到数据回写的效果
		
		if (categorylevel1 != null && categorylevel1 != "") {

			List<AppCategory> appcategoryleve2 = appCategoryService
					.findall(categorylevel1);
			model.addAttribute("appcategoryleve2", appcategoryleve2);
		}

		if (categorylevel2 != null && categorylevel2 != "") {

			List<AppCategory> appcategoryleve3 = appCategoryService
					.findall(categorylevel2);
			model.addAttribute("appcategoryleve3", appcategoryleve3);
		}
		// 2.3根据分页查询数据
		PageBean<AppInfo> pageBean = appInfoService.finall(applistmap);
		model.addAttribute("pageBean", pageBean);
		return "dev/appInfoList";

	}

	// 三级联动
	@RequestMapping("/appinfolistByParemId")
	public @ResponseBody
	List<AppCategory> appinfolistByParemId(@RequestBody Map<String, String> map) {
		String parentid = map.get("levelId");
		List<AppCategory> appcategorylevel = appCategoryService
				.findall(parentid);

		//
		return appcategorylevel;
	}
}
